export default {
  addCount (state, id) {
    // console.log('+', id, state)
    state.cart = state.cart.map(shop => {
      if (shop.id === id) shop.count++
      return shop
    })
  },
  reduceCount (state, id) {
    // console.log('-')
    state.cart = state.cart.map(shop => {
      if (shop.id === id) shop.count--
      return shop
    })
  },
  addToCart (state, prod) {
    // console.log(prod)
    const isExist = state.cart.some(shop => shop.id === prod.id)
    if (isExist) {
      state.cart = state.cart.map(shop => {
        if (shop.id === prod.id) {
          shop.count++
        }
        return shop
      })
    } else {
      state.cart.push({
        ...prod,
        check: true,
        count: 1
      })
    }
  },
  buyNow (state, prod) {
    // console.log(prod)
    const isExist = state.cart.some(shop => shop.id === prod.id)
    if (isExist) {
      state.cart = state.cart.map(shop => {
        if (shop.id === prod.id) {
          shop.count++
        }
        return shop
      })
    } else {
      state.cart.push({
        ...prod,
        check: true,
        count: 1
      })
    }
  },
  toggleAllCheck (state) {
    const isAllCheck = state.cart.every(shop => shop.check)
    // 如果已经全选，那么每一条数据都应该改成false
    // 如果isAllCheck为false，每条数据都改为true
    state.cart = state.cart.map(shop => {
      shop.check = !isAllCheck
      return shop
    })
  },
  toggleItemCheck (state, id) {
    state.cart = state.cart.map(shop => {
      if (shop.id === id) shop.check = !shop.check
      return shop
    })
  },
  deleteProd (state, id) {
    state.cart = state.cart.filter(shop => {
      return shop.id !== id
    })
  }
}
